/* 
 * Copyright (C) 2012 Mark Thompson (mark.artifice@gmail.com)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.artificetech.videolibrary;

import static android.opengl.GLES20.*;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.artificetech.videolibrary.metadata.VideoData;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;

public class MainRenderer implements GLSurfaceView.Renderer {
	private static final float SCROLL_SPEED_X=15.0f;
	private static final float SCROLL_SPEED_Y=-15.0f;
	private static final float FLING_SPEED_X=0.2f;
	private static final float FLING_SPEED_Y=0.2f;
	private float width, height;
    
    private class CarouselGestureListener extends SimpleOnGestureListener {
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
			float dx = (velocityX / width) * FLING_SPEED_X;
    		float dy = (velocityY / height) * FLING_SPEED_Y;
    		
    		carousel.fling(dx+dy);
    		
    		return true;
		}

		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
			float dx = (distanceX / width) * SCROLL_SPEED_X;
    		float dy = (distanceY / height) * SCROLL_SPEED_Y;

    		carousel.deltaOffset(dx+dy);
    		
    		return true;
		}

		public boolean onSingleTapUp(MotionEvent e) {
    		// Normalize coordinates
    		float normalizedX=((e.getX()/width)*2.0f)-1.0f;
    		float normalizedY=1.0f-((e.getY()/height)*2.0f);
    		carousel.click(normalizedX, normalizedY);
    		
    		return true;
		}
    }
    
    CarouselRenderer<VideoData> carousel;
    GestureDetector gestureDetector;
    
	MainRenderer(Context context) {
		carousel=new CarouselRenderer<VideoData>();
		gestureDetector=new GestureDetector(context, new CarouselGestureListener());
	}
	
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// Set background clear colour.
		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		
		glEnable(GL_DEPTH_TEST);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		
		carousel.surfaceCreated();
	}
	
	public void onSurfaceChanged(GL10 gl, int _width, int _height) {
		glViewport(0, 0, _width, _height);
		width=_width; height=_height;
		float aspect=width/height;
		carousel.setView(0.25f, 0.0f, aspect, 1.0f);
	}
	
	public void onDrawFrame(GL10 gl) {
		carousel.render();
	}

	public boolean onTouchEvent(MotionEvent e) {    	
    	switch (e.getAction()) {
    	case MotionEvent.ACTION_DOWN:
    		carousel.startScrolling();
    		break;
    	case MotionEvent.ACTION_UP:
    		carousel.stopScrolling();
    		break;   		
    	}
    	
    	gestureDetector.onTouchEvent(e);

    	return true;
	}
	

}
